import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('../views/login') },
  {
    path: '/home', redirect: '/welcome', component: () => import('../views/home')
    , children: [{ path: '/welcome', component: () => import('../views/welcome.vue') },
      { path: '/users', component: () => import('../views/user') },
      { path: '/roles', component: () => import('../views/roles') },
      { path: '/rights', component: () => import('../views/rights/rights.vue') },
      { path: '/goods', component: () => import('../views/goods') },
      { path: '/goods/add', component: () => import('../views/goods/add.vue') },
      { path: '/orders', component: () => import('../views/orders/index.vue') },
      { path: '/categories', component: () => import('../views/categories') },
      { path: '/params', component: () => import('../views/goods/Params.vue') }]
  }
  
]
 
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//添加路由守卫
router.beforeEach((to, from, next) => {
  let token = store.state.user.token
  if(!token && to.path!=='/login') return next('/login') //如果没有token并且访问的不是login 强制定位登录 
  next()
})
export default router
